﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Xml.Linq;
namespace PC2Phone
{
    public class ApplicationExceptions
    {
        public static void logException(string message, string applicationName, string username)
        {
            string AppPath = System.IO.Path.GetFullPath("Files").Remove(System.IO.Path.GetFullPath("Files").LastIndexOf("bin\\Debug\\Files"));
            // if (!Directory.Exists(Path.Combine(AppPath, "exceptions")))
            //    Directory.CreateDirectory(Path.Combine(AppPath, "exceptions"));

            //if the records file does not exist 
             string saveTo = Path.Combine(AppPath, "exceptions.xml");
            if (!File.Exists(saveTo))
            {

                XElement rootele = new XElement("exceptions",
                               new XElement("exception",
                                   new XElement("message", message),
                                      new XElement("applicationname", applicationName),
                                      new XElement("username", username),
                                       new XElement("date", System.DateTime.Now.ToString())));
                //saving the element
                rootele.Save(saveTo);
            }
            else
            {
                XElement Xdoc = XElement.Load(saveTo);
                XElement newele = new XElement("exception",
                                    new XElement("message", message),
                                       new XElement("applicationname", applicationName),
                                        new XElement("username", username),
                                        new XElement("date", System.DateTime.Now.ToString()));
                Xdoc.Add(newele);
                Xdoc.Save(saveTo);
            }
        }


      
    }
}
